import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;

import farmacia.dao.SubstanciaDao;
import farmacia.entidade.estoque.Substancia;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class Combo extends JFrame {
	ArrayList <Substancia> listaSubstancia ;
	private JPanel contentPane;
	JComboBox cbSubstancia;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Combo frame = new Combo();
					
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws SQLException 
	 */
	public Combo() throws SQLException {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowActivated(WindowEvent e) {
				try {
					carregaCombo();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		cbSubstancia = new JComboBox();
	
		cbSubstancia.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				try {  
		            carregaCombo();  
		        } catch (SQLException ex) {  
		            JOptionPane.showMessageDialog(null, "Erro no método listaCombo() !" + ex);  
		        }
			}
		});
		cbSubstancia.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 try {  
			            carregaCombo();  
			        } catch (SQLException ex) {  
			            JOptionPane.showMessageDialog(null, "Erro no método listaCombo() !" + ex);  
			        }  
			}
		});
		
		cbSubstancia.setBounds(96, 43, 228, 23);
		panel.add(cbSubstancia);
	}
	
	public void carregaCombo() throws SQLException
	{
		
		SubstanciaDao dao;  
        try {  
            dao = new SubstanciaDao();  
            List<Substancia> minhalista = dao.getLista();  
            for (int i = 0; i < minhalista.size(); i++) {  
                cbSubstancia.addItem(minhalista.get(i).getSubstancia());  
            }  
            cbSubstancia = new JComboBox();  
        } catch (SQLException ex) {  
            JOptionPane.showMessageDialog(null,"Erro  ! " + ex);  
        }  
	  
	    }  
		
		
		
        /*DefaultComboBoxModel substancia = new DefaultComboBoxModel();
        for (int i=0;i< this.listaSubstancia.size();i++)
        {
        	substancia.addElement(this.listaSubstancia.get(i).getSubstancia());
        }
        cbSubstancia.setModel(substancia);
        cbSubstancia.updateUI();
	}*/
	
}
